package com.sec.android.app.myfiles.module.search;

import android.text.TextUtils;

import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

public class SearchRecordsCmd extends SimpleCommand {
    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();

        int processId = (Integer) params[0];
        String folderName = (String) params[1];
        String fileName = (String) params[2];

        folderName = folderName != null ? folderName.trim() : "";
        fileName = fileName != null ? fileName.trim() : "";

        if (!TextUtils.isEmpty(folderName) || !TextUtils.isEmpty(fileName)) {
            NavigationManager navigationManager = NavigationManager.getInstance(processId);
            NavigationInfo curInfo = navigationManager.getCurInfo();
            if (curInfo != null) {
                AbsMyFilesFragment fragment = curInfo.getCurFragment();
                if (fragment != null && curInfo.getStorageType() == StorageType.Search) {
                    FileRecord curRecord = curInfo.getCurRecord();
                    ((SearchFileRecord) curRecord).setQuery(fileName);
                    fragment.reload();
                } else {
                    SearchFileRecord record = new SearchFileRecord(folderName, fileName);
                    FileRecord curRecord = curInfo.getCurRecord();
                    record.setPreRecord(curRecord);
                    curInfo.setScreenPath(SamsungAnalyticsLog.ScreenPath.SEARCH);
                    NavigationInfo info = NavigationInfo.getInstance(curInfo, record);
                    navigationManager.enter(info);
                }
            }
        } else {
            Log.e(this, "execute(): Search query is null");
        }
    }
}

